package com.hbx.controller;


import com.hbx.dto.anno.InsertOrUpdateDto;
import com.hbx.service.AnnouncementService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 系统公告表 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/anno")
@Api(tags = "公告处理器")
public class AnnouncementController {

    @Autowired
    private AnnouncementService annoService;

    /**
     * 获取弹出公告信息
     * @return
     */
    @RequestMapping(value = "/getAnnoCommendInfo",method = RequestMethod.GET)
    @ApiOperation("获取弹出公告信息")
    public Result getAnnoCommendInfo(){
        Result r = new Result();
        annoService.getAnnoCommendInfo(r);
        return r;
    }

    /**
     * 确认收到公告
     * @return
     */
    @RequestMapping(value = "/knownAnno",method = RequestMethod.GET)
    @ApiOperation("确认收到公告")
    public Result knownAnno(@RequestParam("annoId") Integer annoId){
        Result r = new Result();
        if (annoId == null){
            r.setCode(201).setMsg("annoId为空");
            return r;
        }
        annoService.knownAnno(annoId, r);
        return r;
    }

    /**
     * 获取所有公告信息
     * @return
     */
    @RequestMapping(value = "/getAnnoList",method = RequestMethod.GET)
    @ApiOperation("获取所有公告信息")
    public Result getAnnoList(){
        Result r = new Result();
        annoService.getAnnoList(r);
        return r;
    }

    /**
     * 删除公告
     * @param annoId
     * @return
     */
    @RequestMapping(value = "/removeAnno",method = RequestMethod.GET)
    @ApiOperation("删除公告")
    public Result removeAnno(@RequestParam("annoId") Integer annoId){
        Result r = new Result();
        annoService.removeAnno(annoId, r);
        return r;
    }

    /**
     * 发布或修改公告
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateAnno",method = RequestMethod.POST)
    @ApiOperation("发布或修改公告")
    public Result updateAnno(@RequestBody @Valid InsertOrUpdateDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        annoService.insertOrUpdateAnno(dto, r);
        return r;
    }
}

